import { Config } from './Config'
import { Snake } from './Snake'
import { Food } from './Food'

export class Timer{
    private timer: NodeJS.Timeout | undefined

    private config
    private snake
    private food
    constructor(config: Config, snake: Snake, food: Food){
        this.config = config
        this.snake = snake
        this.food = food
    }
    start(){
        if(this.timer !== undefined) return
        this.timer = setInterval(() => {
            if(this.snake.isAlive(this.config)){
                this.snake.mobileRule(this.config, this.food)
            }else{
                this.pause()
                alert('=====Game Over=====')
            }
        }, this.config.interval)
    }
    pause(){
        if(this.timer === undefined) return
        clearInterval(this.timer)
        this.timer = undefined
    }
}